package com.gt.web.controller.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gt.app.domain.EsSpiritPsychologyInfo;
import com.gt.app.service.EsSpiritPsychologyInfoService;
import com.gt.common.core.domain.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 精神心理因素表 前端控制器
 * </p>
 *
 * @author caidesheng
 */
@Slf4j
@RestController
@RequestMapping("esSpiritPsychologyInfo")
@Api(tags = "精神心理因素接口")
public class EsSpiritPsychologyInfoController {

    @Autowired
    private EsSpiritPsychologyInfoService esSpiritPsychologyInfoService;

    @ApiOperation(value = "新增精神心理因素")
    @PostMapping(value = "add")
    public AjaxResult add(@RequestBody EsSpiritPsychologyInfo esSpiritPsychologyInfo) {

        try {
            if (esSpiritPsychologyInfo == null){
                return null;
            }
            LambdaQueryWrapper<EsSpiritPsychologyInfo> eq = new LambdaQueryWrapper<EsSpiritPsychologyInfo>().eq(EsSpiritPsychologyInfo::getEsCheckId, esSpiritPsychologyInfo.getEsCheckId());
            EsSpiritPsychologyInfo spiritPsychologyInfo = esSpiritPsychologyInfoService.getOne(eq);
            if (spiritPsychologyInfo != null){
                esSpiritPsychologyInfoService.remove(eq);
            }
            esSpiritPsychologyInfoService.save(esSpiritPsychologyInfo);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    @ApiOperation(value = "根据checkId获取精神心理因素")
    @GetMapping(value = "getSpiritPsychology")
    public AjaxResult getSpiritPsychology(String checkId) {

        LambdaQueryWrapper<EsSpiritPsychologyInfo> eq = new LambdaQueryWrapper<EsSpiritPsychologyInfo>().eq(EsSpiritPsychologyInfo::getEsCheckId, checkId);
        EsSpiritPsychologyInfo spiritPsychologyInfo = esSpiritPsychologyInfoService.getOne(eq);
        return AjaxResult.success(spiritPsychologyInfo);
    }

    //    @ApiOperation(value = "更新精神心理因素")
    @PutMapping(value = "update")
    public AjaxResult update(EsSpiritPsychologyInfo esSpiritPsychologyInfo) {
        try {
            esSpiritPsychologyInfoService.updateById(esSpiritPsychologyInfo);
            return AjaxResult.success(esSpiritPsychologyInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }

    //    @ApiOperation(value = "删除精神心理因素")
    @DeleteMapping(value = "delete")
    public AjaxResult delete(String id) {
        try {
            return AjaxResult.success(esSpiritPsychologyInfoService.removeById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
